import argparse
import os
from datetime import datetime

import cv2


def img_cvt():
    parser = argparse.ArgumentParser(description='图片转素描')
    parser.add_argument('-f', '--file', nargs='?', default='', help='图片文件')
    args, unknown_args = parser.parse_known_args()
    gs_ksize = int(unknown_args[1]) if len(unknown_args) > 1 else 21
    ksize = (gs_ksize, gs_ksize)
    file_path = args.file if args.file != "" else unknown_args[0]
    if ":" in file_path:
        dir_path = os.path.dirname(file_path) + os.sep
    else:
        dir_path = os.getcwd() + os.sep
    img_rgb = cv2.imread(file_path)
    # 将图像转化为灰度图
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
    # Step 2: 灰度反色操作
    img_gray_inv = cv2.bitwise_not(img_gray)
    #  高斯模糊
    img_blur = cv2.GaussianBlur(img_gray_inv, ksize=ksize, sigmaX=0, sigmaY=0)

    inverse_blur = cv2.bitwise_not(img_blur)

    sketch_img = cv2.divide(img_gray, inverse_blur, scale=256.0)
    # cv2.imshow('original', sketch_img)
    # cv2.waitKey(0)
    file_name: str = dir_path + str(round(datetime.now().timestamp())) + ".jpg"
    cv2.imwrite(file_name, sketch_img)


if __name__ == '__main__':
    img_cvt()
